//File: microwire.c
//Author: Buta Vasile
//Description: ----
//last modified: 7:20 PM Tuesday, January 29, 2013

#include <htc.h>
#include "microwire.h"

__CONFIG(0x2FB8);   ////micro specific

/* microwire clock pulse:

            T_CLOCK     
   ________|````````|________ 
   T_CLOCK           T_CLOCK    
*/

//CONFIGURE CLOCK AND DELAY TIMING FROM HERE:
#define T_CLOCK     3      //ms

#define DELAY()     __delay_ms(T_CLOCK);

//send clock pulse
#define CLOCK_PULSE()   DELAY();\
                        PIN_CLK = 1;\
                        DELAY();\
                        PIN_CLK = 0;\
                        DELAY()\

#define LATCH() PIN_DATA = 0;\
                DELAY();DELAY();\
                PIN_LE = 1;\
                DELAY();DELAY();DELAY();\
                PIN_LE = 0;\
                DELAY();DELAY();DELAY()

                
void sendWord_LMX2324(const Bit_t word[])
{
    unsigned int i = 0;
    
    while(1)
    {
        PIN_DATA = word[i];
        CLOCK_PULSE();
        i++;
        if(i == WORD_LENGTH)
            break;
    }
    LATCH();
}

void sendWord_SA7025(const Bit_t word[])
{
    unsigned int i = WORD_LENGTH;
    
    while(1)
    {
        i--;
        PIN_DATA = word[i];
        CLOCK_PULSE();
        if(i == 0)
            break;
    }
    LATCH();
}


void init_hardware()
{
    //init
    TRISB = 0x00;//outbut for port b
    PIN_DATA = 0;
    PIN_CLK = 0;
    PIN_LE = 0;
}

void clear_outs()
{
    PIN_DATA = 0;
    PIN_CLK = 0;
    PIN_LE = 0;
}

void main(void)
{
    init_hardware();
    clear_outs();
    DELAY();
    
#if (LMX2324 == 1)
    
    sendWord_LMX2324(N_REG);    //send N register data
    sendWord_LMX2324(R_REG);    //send R register data
    clear_outs();
    DELAY();
    
#elif (SA7025 == 1)

    sendWord_SA7025(E_REG);
    sendWord_SA7025(D_REG);
    sendWord_SA7025(C_REG);
    sendWord_SA7025(B_REG);
    sendWord_SA7025(A_REG);
    clear_outs();
    DELAY();
    
#endif

    
    for(;;){};//do nothing
}
